﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using SqlSugar;

namespace DAL.Artwork
{

    public class ArtworkDao : BaseDaoPro<Models.Artworks>, IArtworkDao
    {
        string selectSql = @"select 
  *, 
  (
    select 
      count(*) 
    from 
      ArtworkLikes l 
    where 
      a.ArtworkID = l.ArtworkID
  ) as LikeCount,
(select top 1 CreatedAt from ArtworkLikes l where a.ArtworkID = l.ArtworkID order by CreatedAt desc) as LastLikedAt
from 
  Artworks a";

        public override bool Delete(int[] ids)
        {
            foreach (int id in ids)
            {
                DeleteArtworkById(id);
            }
            return true;
        }

        /// <summary>
        /// 通过作品id，删除数据库中一条作品记录
        /// </summary>
        /// <param name="id">作品id</param>
        /// <returns>true为删除成功，反之则失败</returns>
        public bool DeleteArtworkById(int id)
        {
            db.Deleteable<Models.ArtworkLikes>().In(it => it.ArtworkID, id).ExecuteCommand();
            return db.Deleteable<Models.Artworks>().In(it => it.ArtworkID, id).ExecuteCommandHasChange();
        }

        /// <summary>
        /// 通过作品id，获取数据库中一条作品记录
        /// </summary>
        /// <param name="id">作品id</param>
        /// <returns>返回作品实体类</returns>
        public Artworks GetArtworkById(int id)
        {
            return db.SqlQueryable<Models.Artworks>(selectSql)
                .Mapper(it => it.Owner, it => it.OwnerID, it => it.Owner.UserID)
                .Mapper(it=>it.ResumeInfo,it=>it.OwnerID,it=>it.ResumeInfo.OwnerID)
                .First(it => it.ArtworkID == id);
        }

        public override List<Models.Artworks> QueryForList(List<IConditionalModel> searchParams, ref int totalCount, int pageIndex = 1, int pageSize = 15)
        {
            return db.SqlQueryable<Models.Artworks>(selectSql)
                .Mapper(it => it.Owner, it => it.OwnerID, it => it.Owner.UserID)
                .Where(searchParams)
                .ToPageList(pageIndex, pageSize, ref totalCount);
        }
        public override List<Models.Artworks> QueryForList(List<IConditionalModel> searchParams, string orderBy, ref int totalCount, int pageIndex = 1, int pageSize = 15)
        {
            return db.SqlQueryable<Models.Artworks>(selectSql)
                .Mapper(it => it.Owner, it => it.OwnerID, it => it.Owner.UserID)
                .Where(searchParams)
                .OrderBy(orderBy)
                .ToPageList(pageIndex, pageSize, ref totalCount);
        }

        /// <summary>
        /// 从数据库中获取作品列表
        /// </summary>
        /// <param name="pageIndex">当前第几页</param>
        /// <param name="pageSize">每页显示多少条</param>
        /// <returns>
        /// 作品实体类的数组
        /// </returns>
        public List<Artworks> GetList(ref int totalCount, int pageIndex = 1, int pageSize = 12, string orderBy = "CreatedAt Desc")
        {
            return db.SqlQueryable<Models.Artworks>(selectSql)
                .Mapper(it => it.Owner, it => it.OwnerID, it => it.Owner.UserID)
                .OrderBy(orderBy)
                .ToPageList(pageIndex, pageSize, ref totalCount);
        }

        public List<Artworks> GetListByTag(ref int totalCount, string[] tag, int pageIndex = 1, int pageSize = 12, string orderBy = "CreatedAt Desc")
        {
            Expressionable<Models.Artworks> exp = new Expressionable<Artworks>();
            foreach (var item in tag)
            {
                exp.And(it => it.Tag.Contains(item));
            }

            return db.SqlQueryable<Models.Artworks>(selectSql)
                  .Mapper(it => it.Owner, it => it.OwnerID, it => it.Owner.UserID)
                  .Where(exp.ToExpression())
                  .OrderBy(orderBy)
                  .ToPageList(pageIndex, pageSize, ref totalCount);
        }

        public List<Artworks> GetListByUserId(ref int totalCount, int userId, int pageIndex = 1, int pageSize = 12, string orderBy = "CreatedAt Desc")
        {
            Expressionable<Models.Artworks> exp = new Expressionable<Artworks>();
            exp.And(it => it.OwnerID == userId);

            return db.SqlQueryable<Models.Artworks>(selectSql)
                  .Mapper(it => it.Owner, it => it.OwnerID, it => it.Owner.UserID)
                  .Where(exp.ToExpression())
                  .OrderBy(orderBy)
                  .ToPageList(pageIndex, pageSize,ref totalCount);
        }

        /// <summary>
        /// 获取所有作品数量
        /// </summary>
        /// <returns>作品总数量</returns>
        public int GetTotal()
        {
            return db.Queryable<Models.Artworks>().ToList().Count;
        }

        /// <summary>
        /// 插入一条作品数据，到数据库
        /// </summary>
        /// <param name="artwork">作品实体类</param>
        public void InsertArtwork(Artworks artwork)
        {
            db.Insertable<Artworks>(artwork).ExecuteCommandIdentityIntoEntity();
        }

        /// <summary>
        /// 更新数据库中的作品信息
        /// </summary>
        /// <param name="artwork">作品实体类</param>
        /// <returns>true为更新成功，反之则失败</returns>
        public bool UpdateArtwork(Artworks artwork)
        {
            return db.Updateable<Artworks>(artwork).ExecuteCommandHasChange();
        }
    }
}
